package com.pengtu.web.asset.book;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.asset.book.BkLend;
import com.pengtu.service.asset.BookManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/asset/book")
@Results({
		@Result(name = BaseActionSupport.RELOAD, location = "lend.action", type = "redirect"),
		@Result(name = BaseActionSupport.LOGIN, location = "/asset/book/lend.action", type = "redirect") })
public class LendAction extends BaseActionSupport<BkLend> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BookManager bookmanager;

	private String id;
	private BkLend bkLend;
	private Page<BkLend> page = new Page<BkLend>(10);

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public BkLend getModel() {
		// TODO Auto-generated method stub
		return bkLend;
	}

	public Page<BkLend> getPage() {
		return page;
	}

	@Action("/asset/book/lend/lend")
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = bookmanager.findBLFuzzyQuery(page, queryFilter);
		return SUCCESS;
	}

	@Action("/asset/book/lend/lend!input")
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub

		String booksId = Struts2Utils.getParameter("BOOKID");
		bookmanager.Save(bkLend, booksId);
		
		if (StringUtils.isNotEmpty(id)) {
			return LOGIN;
		}
		return RELOAD;
	}

	public String update() {
		String lendBook = Struts2Utils.getParameter("lendBook");
		bookmanager.update(lendBook);
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		logger.debug("delete id = " + id + " lend");
		addActionMessage("删除ID为" + id+ "的借阅记录");
		bookmanager.deleteBL(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			bkLend = bookmanager.findByBLproperty("id", id);
		} else {
			bkLend = new BkLend();
		}

	}

	@Autowired
	public void setBookmanager(BookManager bookmanager) {
		this.bookmanager = bookmanager;
	}

}
